﻿using System;
using System.Collections.Generic;
using Steamworks;
using Terraria.Social.Base;

namespace Terraria.Social.Steam
{
	// Token: 0x02000131 RID: 305
	public class CloudSocialModule : CloudSocialModule
	{
		// Token: 0x06000D29 RID: 3369 RVA: 0x0000D37D File Offset: 0x0000B57D
		public override void Initialize()
		{
			base.Initialize();
		}

		// Token: 0x06000D2A RID: 3370 RVA: 0x00006B58 File Offset: 0x00004D58
		public override void Shutdown()
		{
		}

		// Token: 0x06000D2B RID: 3371 RVA: 0x003F7148 File Offset: 0x003F5348
		public override IEnumerable<string> GetFiles()
		{
			object obj = this.ioLock;
			IEnumerable<string> result;
			lock (obj)
			{
				int fileCount = SteamRemoteStorage.GetFileCount();
				List<string> list = new List<string>(fileCount);
				for (int i = 0; i < fileCount; i++)
				{
					int num;
					list.Add(SteamRemoteStorage.GetFileNameAndSize(i, ref num));
				}
				result = list;
			}
			return result;
		}

		// Token: 0x06000D2C RID: 3372 RVA: 0x003F71B4 File Offset: 0x003F53B4
		public override bool Write(string path, byte[] data, int length)
		{
			object obj = this.ioLock;
			bool result;
			lock (obj)
			{
				UGCFileWriteStreamHandle_t ugcfileWriteStreamHandle_t = SteamRemoteStorage.FileWriteStreamOpen(path);
				uint num = 0u;
				while ((ulong)num < (ulong)((long)length))
				{
					int num2 = (int)Math.Min(1024L, (long)length - (long)((ulong)num));
					Array.Copy(data, (long)((ulong)num), this.writeBuffer, 0L, (long)num2);
					SteamRemoteStorage.FileWriteStreamWriteChunk(ugcfileWriteStreamHandle_t, this.writeBuffer, num2);
					num += 1024u;
				}
				result = SteamRemoteStorage.FileWriteStreamClose(ugcfileWriteStreamHandle_t);
			}
			return result;
		}

		// Token: 0x06000D2D RID: 3373 RVA: 0x003F724C File Offset: 0x003F544C
		public override int GetFileSize(string path)
		{
			object obj = this.ioLock;
			int fileSize;
			lock (obj)
			{
				fileSize = SteamRemoteStorage.GetFileSize(path);
			}
			return fileSize;
		}

		// Token: 0x06000D2E RID: 3374 RVA: 0x003F7290 File Offset: 0x003F5490
		public override void Read(string path, byte[] buffer, int size)
		{
			object obj = this.ioLock;
			lock (obj)
			{
				SteamRemoteStorage.FileRead(path, buffer, size);
			}
		}

		// Token: 0x06000D2F RID: 3375 RVA: 0x003F72D4 File Offset: 0x003F54D4
		public override bool HasFile(string path)
		{
			object obj = this.ioLock;
			bool result;
			lock (obj)
			{
				result = SteamRemoteStorage.FileExists(path);
			}
			return result;
		}

		// Token: 0x06000D30 RID: 3376 RVA: 0x003F7318 File Offset: 0x003F5518
		public override bool Delete(string path)
		{
			object obj = this.ioLock;
			bool result;
			lock (obj)
			{
				result = SteamRemoteStorage.FileDelete(path);
			}
			return result;
		}

		// Token: 0x040011B0 RID: 4528
		private const uint WRITE_CHUNK_SIZE = 1024u;

		// Token: 0x040011B1 RID: 4529
		private object ioLock = new object();

		// Token: 0x040011B2 RID: 4530
		private byte[] writeBuffer = new byte[1024];
	}
}
